package cn.bigcontainer.alt.meta.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoVelocityKit;
import cn.bigcontainer.alt.meta.dao.MDao;
import cn.bigcontainer.alt.meta.dto.MAppDto;
import cn.bigcontainer.alt.meta.mapper.MAppMapper;
import cn.bigcontainer.alt.meta.mapper.MObjectMapper;
import cn.bigcontainer.alt.meta.po.MViewProperty;
import cn.bigcontainer.alt.meta.service.ListPropertyService;
import cn.bigcontainer.alt.meta.service.ObjectInputorService;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author jiaod
 *
 */
@Service
@Transactional(rollbackFor = { Exception.class })
@Primary
@Slf4j
public class ListPropertyServiceImpl implements ListPropertyService {

	@Resource
	private MAppMapper appMapper;
	
	@Resource
	private MObjectMapper objectMapper;
	
	@Resource
	private MDao dao;
	
	@Resource
	private ObjectInputorService objectInputorService;
	
	public Map<String, Object> getListPropertyValue(String id, String[] listPropertyData, BigMap<String, Object> req, DataGetter dg) {
		Map<String, Object> result = new HashMap<String,Object>();
		for (String lpData : listPropertyData) {
			String[] lpKeyValues = lpData.split(":");
			List<Map<String, Object>> data = dg.getValue(id, lpKeyValues[0], lpKeyValues[1], req);
			String nameString = "";
			for (Map<String, Object> valueMap : data) {
				if (!"".equals(nameString)) {
					nameString += ",";
				}
				nameString += valueMap.get("name");
			}
			result.put(lpData, nameString);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListPropertyValue(String id, String[] listPropertyData) {
		
		return getListPropertyValue(id, listPropertyData, null, new DataGetter () {
			
			@Override
			public List<Map<String, Object>> getValue(String appId, String propertyId, String keyValue,
					BigMap<String, Object> req) {
				return objectInputorService.getValue(id, propertyId, keyValue, null);
			}});
	}
	
	@Resource
	private WoVelocityKit velocity;

	@Resource
	private MDao mDao;
	
	@Override
	public Map<String, Object> querySql(String id, String[] listPropertyData, BigMap<String, Object> req) {
		return getListPropertyValue(id, listPropertyData, req, new DataGetter () {

			@Override
			public List<Map<String, Object>> getValue(String appId, String propertyId, String keyValue,
					BigMap<String, Object> req) {
				MAppDto appDto = new MAppDto(appMapper.selectById(appId));
				MViewProperty viewProperty = appDto.getViewProperty(propertyId);
				String sqlString = velocity.parse(viewProperty.getAjaxSql(), req.bigPut("selectedIds", keyValue));
				return mDao.queryList(sqlString);
			}});
	}
	
	interface DataGetter {
		
		List<Map<String, Object>> getValue(String appId, String propertyId, String keyValue, BigMap<String, Object> req);
		
	}
}
